#include <iostream>
#include <string>
#include<atomic>
#include<list>
#include<thread>

using namespace std;

/*
使用lock_guard实现临界代码段的互斥访问
lock_guard<mutex> lock(mtx);
Count++;
*/
volatile std::atomic_bool isReady = {false};
volatile std::atomic_int number = {0};

void task()
{
    while(!isReady)
    {
        // 让线程让出当前的CPU时间片，等待下一次调度
        this_thread::yield();
    }
    for (int i = 0; i < 100;i++)
    {
        number++;
    }
}
int main()
{
    list<thread> tlist;
    for (int i = 0; i < 10;i++)
    {
        tlist.push_back(thread(task));
    }

    // 让主线程睡眠三秒
    this_thread::sleep_for(chrono::seconds(3));
    cout << "number = " << number << endl;
    isReady = true;
    cout << "number = " << number << endl;
    for(thread &t:tlist)
    {
        t.join();
    }
    cout << "number = " << number << endl;
    return 0;
}
/*output::
number = 0
number = 1000
number = 1000
*/